package edu.cebanc.spring.biblioteca.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.domain.Genre;

@Repository("genreDAO")
@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
public class GenreDAOimpl implements GenreDAO {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void saveGenre(Genre genre) {
		sessionFactory.getCurrentSession().saveOrUpdate(genre);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Genre> listGenres() {
		return (List<Genre>) sessionFactory.getCurrentSession().createCriteria(Genre.class).list();
	}

	@Override
	public Genre getGenre(int id) {
		return (Genre) sessionFactory.getCurrentSession().load(Genre.class, id);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void delGenre(Genre genre) {
		sessionFactory.getCurrentSession().delete(genre);
	}
}
